using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class BackgroundAnimationController : MonoBehaviour
{
	[SerializeField]
	private RectTransform _cloud01;

	[SerializeField]
	private RectTransform _cloud02;

	[SerializeField]
	private RectTransform _cloud03;

	[SerializeField]
	private RectTransform _viewPort;

	[SerializeField]
	private CanvasScaler _canvasScaler;

	private void Start()
	{
		RandomizeCloudPositions();
		Rect rect = _viewPort.rect;
		Vector2 referenceResolution = _canvasScaler.referenceResolution;
		float num = referenceResolution.x * rect.width / rect.height * referenceResolution.y / referenceResolution.x;
		float num2 = num * 0.00625f;
		Vector3 lossyScale = _viewPort.lossyScale;
		float speed = num2 * lossyScale.x * 2f;
		StartCoroutine(MoveCloud(_cloud01, speed));
		StartCoroutine(MoveCloud(_cloud02, speed));
		StartCoroutine(MoveCloud(_cloud03, speed));
	}

	private void RandomizeCloudPositions()
	{
		UnityEngine.Random.InitState((int)DateTime.Now.Ticks);
		Rect rect = _viewPort.rect;
		Vector2 referenceResolution = _canvasScaler.referenceResolution;
		float num = referenceResolution.x * rect.width / rect.height * referenceResolution.y / referenceResolution.x;
		float y = referenceResolution.y;
		float width = _cloud01.rect.width;
		float num2 = y * 0.05f;
		float num3 = rect.height * 0.1f;
		float x = num * 0.4f + width;
		float x2 = num + width;
		float x3 = num * 1.6f + width;
		RandomizeCloudPosition(_cloud01, x, 0f - num3 + num2, 0f - num3 - num2);
		RandomizeCloudPosition(_cloud02, x2, 0f - num3 + num2, 0f - num3 - num2);
		RandomizeCloudPosition(_cloud03, x3, 0f - num3 + num2, 0f - num3 - num2);
	}

	private static void RandomizeCloudPosition(RectTransform cloud, float x, float yMin, float yMax)
	{
		cloud.anchoredPosition = new Vector2(x, UnityEngine.Random.Range(yMin, yMax));
	}

	private IEnumerator MoveCloud(RectTransform cloud, float speed)
	{
		Vector3 cloudSpeed = new Vector3(0f - speed, 0f, 0f);
		while (base.gameObject.activeInHierarchy)
		{
			yield return null;
			cloud.Translate(cloudSpeed * Time.deltaTime);
			Vector2 anchoredPosition = cloud.anchoredPosition;
			if (anchoredPosition.x < 0f)
			{
				Rect rect = _viewPort.rect;
				Vector2 referenceResolution = _canvasScaler.referenceResolution;
				float num = referenceResolution.x * rect.width / rect.height * referenceResolution.y / referenceResolution.x;
				float y = referenceResolution.y;
				float width = _cloud01.rect.width;
				float num2 = y * 0.05f;
				float num3 = rect.height * 0.1f;
				float x = num * 1.6f + width;
				RandomizeCloudPosition(cloud, x, 0f - num3 + num2, 0f - num3 - num2);
			}
		}
	}
}
